package com.anthony.app.module.film;

import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.anthony.app.R;
import com.anthony.app.common.base.AbsBaseFragment;
import com.anthony.app.common.injection.component.ActivityComponent;
import com.anthony.app.common.utils.ToastUtils;
import com.anthony.app.common.widgets.ViewDisplay;
import com.anthony.app.module.adapter.MyViewpagerAdapter;
import com.anthony.app.module.tab.HomeFragment;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * ============================================================
 * 版权： xx有限公司 版权所有（c）2016
 * <p>
 * 作者：Loofer
 * 版本：1.0
 * 创建日期 ：2016/10/21 22:05.
 * 描述：
 * <p>
 * 注:如果您修改了本类请填写以下内容作为记录，如非本人操作劳烦通知，谢谢！！！
 * Modified Date Modify Content:
 * <p>
 * ==========================================================
 */

public class FilmFragment extends AbsBaseFragment implements ViewPager.OnPageChangeListener {


    @Inject
    ViewDisplay mViewDisplay;
    @Inject
    ToastUtils toastUtils;

    @BindView(R.id.tablayout)
    TabLayout tablayout;
    @BindView(R.id.appbarlayout)
    AppBarLayout appbarlayout;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.coordinatorlayout)
    CoordinatorLayout coordinatorlayout;
    // TabLayout中的tab标题
    private String[] mTitles;
    // 填充到ViewPager中的Fragment
    private List<Fragment> mFragments;
//    private FilmLiveFragment filmLiveFragment;
//    private FilmTop250Fragment filmTop250Fragment;
    //private FilmGodFragment filmGodFragment;
    private MyViewpagerAdapter mViewPagerAdapter;

    public static FilmFragment newInstance() {
        Bundle args = new Bundle();
        FilmFragment fragment = new FilmFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getContentViewID() {
        return R.layout.fragment_film;
    }

    @Override
    protected void initViewsAndEvents(View rootView) {
        mTitles = getResources().getStringArray(R.array.tab_film);
        //初始化填充到ViewPager中的Fragment集合
        mFragments = new ArrayList<>();
//        filmLiveFragment=FilmLiveFragment.newInstance();
//        filmTop250Fragment=FilmTop250Fragment.newInstance() ;
        // filmGodFragment=FilmGodFragment.newInstance();
        mFragments.add(new HomeFragment());
        // mFragments.add(filmGodFragment);
        mFragments.add(new HomeFragment());


        // 初始化ViewPager的适配器，并设置给它
        mViewPagerAdapter = new MyViewpagerAdapter(getChildFragmentManager(), mTitles, mFragments);
        viewpager.setAdapter(mViewPagerAdapter);
        // 设置ViewPager最大缓存的页面个数
        viewpager.setOffscreenPageLimit(3);
        // 给ViewPager添加页面动态监听器（为了让Toolbar中的Title可以变化相应的Tab的标题）
        viewpager.addOnPageChangeListener(this);

        tablayout.setTabGravity(TabLayout.GRAVITY_FILL);
        tablayout.setTabMode(TabLayout.MODE_FIXED);
//        tablayout.setSelectedTabIndicatorColor(ThemeUtils.getThemeColor());
//        tablayout.setTabTextColors(getResources().getColor(R.color.text_gray_6),ThemeUtils.getThemeColor());
        // 将TabLayout和ViewPager进行关联，让两者联动起来
        tablayout.setupWithViewPager(viewpager);
        // 设置Tablayout的Tab显示ViewPager的适配器中的getPageTitle函数获取到的标题
        tablayout.setTabsFromPagerAdapter(mViewPagerAdapter);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    protected void initDagger2(ActivityComponent activityComponent) {
        activityComponent.inject(this);
    }
}
